package com.example.demo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class RedisDemoHash {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            test(jedis);
        }
    }

    private static void test(Jedis jedis) {
        jedis.flushAll();
        System.out.println("hset 和 hget 使用");
        HashMap<String,String> hash = new HashMap<>();
        hash.put("f2","v2");
        hash.put("f3","v3");
        hash.put("f4","v4");
        jedis.hset("key",hash);
        jedis.hset("key","f1","v1");
        System.out.println("key f1: " +jedis.hget("key", "f1"));
        System.out.println("key f5: " +jedis.hget("key", "f5"));

        System.out.println("hexists 使用");
        Boolean result = jedis.hexists("key","f1");
        System.out.println("key f1 result: " + result);
        result = jedis.hexists("key","f5");
        System.out.println("key f5 result: " + result);

        System.out.println("hkeys 和 hvals 使用");

        Set<String> hkeys = jedis.hkeys("key");
        System.out.println("hkeys： " + hkeys);
        List<String> hvals = jedis.hvals("key");
        System.out.println("hvals: " +hvals);

        System.out.println("hdel 使用");
        jedis.hdel("key","f1");
        result = jedis.hexists("key","f1");
        System.out.println("key f1 result: " + result);

        System.out.println("hmset 和 hmget 使用");
        List<String> hmget = jedis.hmget("key", "f1", "f2", "f3");
        System.out.println("hmget key: " + hmget);
    }
}
